package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.data.DataResult;
import com.dongdong.data.ResponseStatusEnum;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbSpecificationOption;
import com.dongdong.service.SpecificationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/10 11:19
 * @Version 1.0
 **/
@Controller
@RequestMapping("specificationController")
public class SpecificationController {

    private static final Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference
    private SpecificationService specificationService;

    @RequestMapping("list")
    public String list(){
        return "admin/specification";
    }

    @RequestMapping("specificationList")
    @ResponseBody
    public DataResult specificationList(String specName){
        try {
            List<TbSpecification> tbSpecificationList = specificationService.getSpecificationList(specName);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(tbSpecificationList);
        }catch (Exception e){
            logger.error("查询失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }

    }

    @RequestMapping("SaveSpecification")
    @ResponseBody
    public DataResult SaveSpecification(TbSpecification tbSpecification,String optionListJson){
        try {
            List<TbSpecificationOption> list = JSONObject.parseArray(optionListJson,TbSpecificationOption.class);
            int i = specificationService.SaveSpecification(tbSpecification,list);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("增加失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public DataResult toUpdate(Long id){
        try {
            TbSpecification s = specificationService.toUpdate(id);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(s);
        }catch (Exception e){
            logger.error("回显错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public DataResult updateInfo(TbSpecification tbSpecification,String optionListJson){
        try {
            List<TbSpecificationOption> list = JSONObject.parseArray(optionListJson,TbSpecificationOption.class);
            int i = specificationService.updateSpecification(tbSpecification,list);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("修改失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteBatch")
    @ResponseBody
    public DataResult deleteBatch(@RequestParam("ids[]") Long[] ids){
        try {
            int i = specificationService.deleteBatch(ids);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("删除失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

}

